﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;

namespace More.Engine.Query
{
    public static class InqlExtensions
    {
        public static IQueryable<T> Inql<T>(this IQueryable<T> t, Dictionary<InqlExpressionType, string> expressions,
            IDictionary<string, object> variables)
        {
            var query = new InqlQuery { Variables = variables, Expressions = expressions };
            return query.Filter(t);
        }
        public static IQueryable<T> Inql<T>(this IQueryable<T> t, ExpandoObject expressions,
            IDictionary<string, object> variables)
        {
            var dictionaryExpressions = expressions.ToDictionary(p => (InqlExpressionType)Enum.Parse(typeof(InqlExpressionType), p.Key), p => p.Value.ToString());
            return Inql(t, dictionaryExpressions, variables);
        }
        public static IQueryable<T> Inql<T>(this IEnumerable<T> t, Dictionary<InqlExpressionType, string> expressions,
            IDictionary<string, object> variables)
        {
            return Inql(t.AsQueryable(), expressions, variables);
        }
        public static IQueryable<T> Inql<T>(this IEnumerable<T> t, ExpandoObject expressions,
            IDictionary<string, object> variables)
        {
            return Inql(t.AsQueryable(), expressions, variables);
        }
    }
}
